from collections import OrderedDict

from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
#分页
class LimitPagination(LimitOffsetPagination):
    max_limit = 10  # 最大限制默认是None
    default_limit =10  # 设置每一页显示多少条
    limit_query_param = 'limit'  # 往后取几条
    offset_query_param = 'offset'  # 当前所在的位置
    def get_paginated_response(self,status,msg,data):
        return Response(OrderedDict([
            ('status', status),
            ('msg', msg),
            ('count', self.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('data', data)
        ]))

#抛出异常
class DataFailed(APIException):
    status_code = 201
    default_detail = _('Error data structure.')
    default_code = 'data_failed'